package 集合.set集合的特点;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class text {
    public static void main(String[] args) {
        Set<student> set = new HashSet<>();

        student s1 = new student("测试一号", 11);
        student s2 = new student("测试二号", 22);
        student s3 = new student("测试三号", 33);
        student s4 = new student("测试四号", 44);
        student s5 = new student("测试二号", 22);
        student s6 = new student("测试三号", 33);

        //将集合对象添加到元素对象中去
        set.add(s1);
        set.add(s2);
        set.add(s3);
        set.add(s4);
        set.add(s5);
        set.add(s6);

        //遍历集合

        System.out.println(set);
        System.out.println("---------------------");


        //通过迭代器遍历set集合
        //通过集合对象获取其对应的迭代器对象
        Iterator<student> it = set.iterator();
        //判断迭代器中是否有元素
        while (it.hasNext()) {
            //如果有，就获取元素
            student s = it.next();
            System.out.println(s);
        }
        System.out.println("-----------------");

        //通过增强for循环遍历集合
        for (student student : set) {
            System.out.println(student);
        }
    }
}
